<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Oracle数据库基础知识-2 | Kylin</title><meta name="keywords" content="基础,数据库,Oracle,sql"><meta name="author" content="Kylin"><meta name="copyright" content="Kylin"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="单行函数 字符函数  select * from emp;  select INITCAP(ename) from emp;–initcap函数将首字母大写">
<meta property="og:type" content="article">
<meta property="og:title" content="Oracle数据库基础知识-2">
<meta property="og:url" content="https://www.codekylin.cn/46714.html">
<meta property="og:site_name" content="Kylin">
<meta property="og:description" content="单行函数 字符函数  select * from emp;  select INITCAP(ename) from emp;–initcap函数将首字母大写">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://qiniu.codekylin.cn/github/img/img/博客封面12.png">
<meta property="article:published_time" content="2019-09-15T08:46:01.000Z">
<meta property="article:modified_time" content="2020-11-16T02:26:42.000Z">
<meta property="article:author" content="Kylin">
<meta property="article:tag" content="基础">
<meta property="article:tag" content="数据库">
<meta property="article:tag" content="Oracle">
<meta property="article:tag" content="sql">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://qiniu.codekylin.cn/github/img/img/博客封面12.png"><link rel="shortcut icon" href="https://qiniu.codekylin.cn/img/20200807181548.png"><link rel="canonical" href="https://www.codekylin.cn/46714"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//www.google-analytics.com" crossorigin=""/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><meta name="google-site-verification" content="gzeyWstt6NoTZKh7YFYNLNziL8HIZ8YH2Ug7xTDX5-Y"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?f76c34daefe747deee7c7be3ead2ba80";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-159334016-1"></script><script>window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-159334016-1');
</script><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: {"limitDay":90,"position":"top","messagePrev":"自上次更新以来已经","messageNext":"天，文章的内容可能已过时或存在差异。"},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50000,"languages":{"author":"作者: Kylin","link":"链接: ","source":"来源: Kylin","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#FF0000","bgDark":"#2d3035","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-11-16 10:26:42'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
          const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
          const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
          const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified

          if (t === undefined) {
            if (isLightMode) activateLightMode()
            else if (isDarkMode) activateDarkMode()
            else if (isNotSpecified || hasNoSupport) {
              const now = new Date()
              const hour = now.getHours()
              const isNight = hour <= 6 || hour >= 18
              isNight ? activateDarkMode() : activateLightMode()
            }
            window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
              if (saveToLocal.get('theme') === undefined) {
                e.matches ? activateDarkMode() : activateLightMode()
              }
            })
          } else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    })(window)</script><link rel="stylesheet" href="https://qiniu.codekylin.cn/github/img/img/custom.css"><link rel="stylesheet" href="//at.alicdn.com/t/font_1993646_z05rabxf05h.css"><link rel="stylesheet" href="https://qiniu.codekylin.cn/github/img/img/icon.css"><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="Kylin" type="application/atom+xml">
</head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" data-lazy-src="https://qiniu.codekylin.cn/img/20200807181526.jpg" onerror="onerror=null;src='https://qiniu.codekylin.cn/github/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">362</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">427</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">101</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://qiniu.codekylin.cn/github/img/img/博客封面12.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Kylin</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Oracle数据库基础知识-2</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-09-15T08:46:01.000Z" title="发表于 2019-09-15 16:46:01">2019-09-15</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-11-16T02:26:42.000Z" title="更新于 2020-11-16 10:26:42">2020-11-16</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/Oracle/">Oracle</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>7分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="单行函数"><a href="#单行函数" class="headerlink" title="单行函数"></a>单行函数</h2><ul>
<li><p>字符函数</p>
</li>
<li><p><code>select * from emp;</code></p>
</li>
<li><p><code>select INITCAP(ename) from emp;</code>–initcap函数将首字母大写</p>
<span id="more"></span></li>
<li><p><code>select lower(ename)from emp;</code>–lower 字母小写</p>
</li>
<li><p><code>select replace(ename,&#39;S&#39;,&#39;M&#39;)  from emp;</code>–replace 替换</p>
</li>
</ul>
<table>
<thead>
<tr>
<th align="center">函数</th>
<th align="center">功能</th>
<th align="center">示例</th>
<th align="center">结果</th>
</tr>
</thead>
<tbody><tr>
<td align="center">INITCAP(char)</td>
<td align="center">首字母大写</td>
<td align="center">initcap(‘hello’)</td>
<td align="center">Hello</td>
</tr>
<tr>
<td align="center">LOWER(char)</td>
<td align="center">转换为小写</td>
<td align="center">lower(‘FUN’)</td>
<td align="center">fun</td>
</tr>
<tr>
<td align="center">UPPER(chaor)</td>
<td align="center">转换为大写</td>
<td align="center">upper(‘sun’)</td>
<td align="center">SUN</td>
</tr>
<tr>
<td align="center">LTRIM(char,set)</td>
<td align="center">左裁剪</td>
<td align="center">ltrim(‘xyzadams’,’xyz’)</td>
<td align="center">adams</td>
</tr>
<tr>
<td align="center">RTRIM(char,set)</td>
<td align="center">右裁剪</td>
<td align="center">rtrim(‘xyzadams’,’ams’)</td>
<td align="center">xyzad</td>
</tr>
<tr>
<td align="center">TRANSLATE(char,from,to)</td>
<td align="center">按字符翻译</td>
<td align="center">translate(‘jack’,’abcd’,’1234’)</td>
<td align="center">j13k</td>
</tr>
<tr>
<td align="center">REPLACE(char,search_str,replace_str)</td>
<td align="center">字符串替换</td>
<td align="center">replace(‘jack and jue’,’j’,’bl’)</td>
<td align="center">black and blue</td>
</tr>
<tr>
<td align="center">INSTR(char,substr[,pos])</td>
<td align="center">查找子串的位置</td>
<td align="center">instr(‘worldwide’,’d’)</td>
<td align="center">5</td>
</tr>
<tr>
<td align="center">SUBSTR(char,pos,len)</td>
<td align="center">取字符串</td>
<td align="center">substr(‘abcdefg’,3,2)</td>
<td align="center">cd</td>
</tr>
<tr>
<td align="center">CONCAT(char1,char2)</td>
<td align="center">连接字符串</td>
<td align="center">concat(‘Hello’,’world’)</td>
<td align="center">Helloworld</td>
</tr>
</tbody></table>
<ul>
<li><p>数值函数</p>
</li>
<li><p><strong>伪表：dual</strong></p>
</li>
<li><p><code>select * from dual</code></p>
</li>
<li><p><code>select abs(-3) 绝对值，ceil(3.1415926)向上取整,floor(3.1415926)向下取整,power(2,3)幂,round(3.4)四舍五入 from dual</code></p>
</li>
</ul>
<table>
<thead>
<tr>
<th align="center">函数</th>
<th align="center">功能</th>
<th align="center">示例</th>
<th align="center">结果</th>
</tr>
</thead>
<tbody><tr>
<td align="center">ABS(n)</td>
<td align="center">取绝对值</td>
<td align="center">abs(-15)</td>
<td align="center">15</td>
</tr>
<tr>
<td align="center">CEIL(n)</td>
<td align="center">向上取整</td>
<td align="center">ceil(44.778)</td>
<td align="center">45</td>
</tr>
<tr>
<td align="center">SIN(n)</td>
<td align="center">正弦</td>
<td align="center">sin(1.571)</td>
<td align="center">.999999979</td>
</tr>
<tr>
<td align="center">COS(n)</td>
<td align="center">余弦</td>
<td align="center">cos(0)</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">SIGN(n)</td>
<td align="center">取符号</td>
<td align="center">sign(-32)</td>
<td align="center">-1</td>
</tr>
<tr>
<td align="center">FLOOR(n)</td>
<td align="center">向下取整</td>
<td align="center">floor(100.2)</td>
<td align="center">100</td>
</tr>
<tr>
<td align="center">POWER(m,n)</td>
<td align="center">m的n次幂</td>
<td align="center">power(4,2)</td>
<td align="center">16</td>
</tr>
<tr>
<td align="center">MOD(m,n)</td>
<td align="center">取余数</td>
<td align="center">mod(10,3)</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">ROUND(m,n)</td>
<td align="center">四舍五入</td>
<td align="center">round(100.256,2)</td>
<td align="center">100.26</td>
</tr>
<tr>
<td align="center">TRUNC(m,n)</td>
<td align="center">截断</td>
<td align="center">trunc(100.256,2)</td>
<td align="center">100.25</td>
</tr>
<tr>
<td align="center">SQRT(n)</td>
<td align="center">平方根</td>
<td align="center">sqrt(4)</td>
<td align="center">2</td>
</tr>
</tbody></table>
<ul>
<li>日期函数</li>
<li><code>select months_between(&#39;13-12月-2016&#39;,&#39;13-10月-2016&#39;) from dual</code>–months_between两个日期之间的月份数</li>
</ul>
<table>
<thead>
<tr>
<th align="center">函数</th>
<th align="center">功能</th>
<th align="center">示例</th>
<th align="center">结果</th>
</tr>
</thead>
<tbody><tr>
<td align="center">MONTHS_BETWEEN</td>
<td align="center">返回两个日期间的月份</td>
<td align="center">months_between(‘04-11月-05’,’11-1月-01’)</td>
<td align="center">57.7741935</td>
</tr>
<tr>
<td align="center">ADD_MONTHS</td>
<td align="center">返回把月份数加到日期上的新日期</td>
<td align="center">add_months(‘06-2月-03’,1) add_months(‘06-2月-03’,-1)</td>
<td align="center">06-3月-03 06-1月-03</td>
</tr>
<tr>
<td align="center">NEXT_DAY</td>
<td align="center">返回指定日期后的星期对应的新日期</td>
<td align="center">next_day(‘06-2月-03，’星期一’)</td>
<td align="center">10-2月-03</td>
</tr>
<tr>
<td align="center">LAST_DAY</td>
<td align="center">返回指定日期所在月的最后一天</td>
<td align="center">last—_day(‘06-2月-03’)</td>
<td align="center">28-月-03</td>
</tr>
<tr>
<td align="center">ROUND</td>
<td align="center">按指定格式对日期进行四舍五入</td>
<td align="center">round(to_date(‘13-2月-03’),’YEAR’)</td>
<td align="center">01-1月-03</td>
</tr>
<tr>
<td align="center">TRUNC</td>
<td align="center">对日期按指定方式进行截断</td>
<td align="center">trunc(to_date(‘06-2月-03’)，’YEAR’)</td>
<td align="center">01-1月-03</td>
</tr>
</tbody></table>
<h2 id="多行函数"><a href="#多行函数" class="headerlink" title="多行函数"></a>多行函数</h2><ul>
<li><strong>max: max(字段名) 返回此字段的最大值</strong></li>
<li><strong>min：min(字段名) 返回此字段的最小值</strong></li>
<li><strong>avg：avg(字段名) 返回平均值</strong></li>
<li><strong>sum：sum（字段名） 返回字段的和</strong></li>
<li><strong>count：</strong><ul>
<li><strong>count(*)，用来查询表中有多少条记录</strong></li>
<li><strong>count（字段名），用来查询某个字段有值的个数</strong></li>
<li><strong>count(distinct 字段名),可以先去除重复再计数</strong></li>
</ul>
</li>
<li>注意：<ul>
<li><strong><mark>多行函数不能和普通字段直接出现在查询语句中，除非group by</mark></strong> </li>
<li><strong><mark>多行函数和单行函数不能直接出现在查询语句中，除非group by</mark></strong>  </li>
</ul>
</li>
<li>实例<ul>
<li>查看员工的最高工资<br><code>select max(sal),ename from emp</code>–多行函数不能和普通字段直接出现在查询语句中，除非group by<br><code>select max(sal)，lower(ename)from emp</code>–多行函数和单行函数不能直接出现在查询语句中，除非<code>group by select ename from emp</code></li>
<li>查看员工的最低工资<br><code>select min(sal) from emp</code></li>
<li>查看员工的平均工资<br><code>select avg(sal) from emp</code></li>
<li>查看所有的员工工资之和<br><code>select sum(sal) from emp</code></li>
<li>查询公司有多少员工<br><code>select * from emp</code><br><code>select count(*) from emp</code>–使用count(*)来查看一张表中有多少条记录</li>
<li>查询有津贴的员工人数<br><code>select count(comm) from emp</code>–使用count(字段名),查询该字段有值的记录数<br><code>select count(ename) from emp</code></li>
<li>查询公司有多少工作种类<br><code>select count(job) from emp</code><br><code>select  count(distinct job) from emp</code></li>
</ul>
</li>
</ul>
<h2 id="转换函数"><a href="#转换函数" class="headerlink" title="转换函数"></a>转换函数</h2><p><strong>转换函数:在转换的时候改变的是数据的类型，数据内容不会改变，可以指定格式。</strong></p>
<ul>
<li>to_number：将数字字符转换为数字类型的数值，to_number(数字字符)</li>
<li>to_char：将数字转换为字符类型，将日期转换为字符类型 to_char(数字/日期)</li>
<li>to_date：将字符类型的日期转换为日期类型： to_date(char)</li>
<li>实例<ul>
<li>number–&gt;char 转换的时候使用的是默认格式，<br><code>select  to_char(123) from dual</code>–将数字转换为字符类型，使用to_char(number)<br><code>select sal, to_char(sal) from emp</code></li>
<li><strong>number–&gt;char 使用指定的格式</strong><br>to_char(number,’格式’)<br><code>select to_char(sal),to_char(sal,&#39;L999,999,999&#39;) from emp</code><br><code>select to_char(sal),to_char(sal,&#39;L0000.00&#39;) from emp</code><br>  <strong>$代表美元符，9代码数字占位。例如，L999,999,999表示三位一组的显示方式L代表人民币符号，0代表保留数字个数，不足使用0补充。</strong></li>
<li>char—&gt;number<br>to_number(数字字符)<br><code>select to_number(&#39;123&#39;) from dual</code></li>
<li>char—&gt;date，转换的字符必须是日期格式的字符串，默认格式 dd-mm-yyyy</li>
<li>注意：因为字符串有很多，所以在字符转换为日期的时候，需要指定格式，因为日期是具备一定格式的字符组合。字符转换为日期的时候，指定的格式为字符的日期顺序，无需指定间隔符。<strong>yyyy：表示年  mm:表示月   dd表示日</strong><br><code>select to_date(&#39;05-12月-2016&#39;) from dual;</code><br><code>select to_date(&#39;2016-05-12&#39;,&#39;yyyy-mm-dd&#39;) from dual</code>–使用指定的格式将指定的日期字符串转换为日期<br><code>select to_date(&#39;12-05-2016&#39;,&#39;mm/dd/yyyy&#39;) from dual</code></li>
<li>date—&gt;char</li>
<li>注意：因为日期本身就具备一定的格式在不是指定格式的情况下会默认使用dd-mm-yyyy格式显示数据。指定的格式会作为日期转换为字符串类型的显示格式存在。例如：<br>yyyy-mm-dd’   ‘2016-12-05’<br>yyyy/mm/dd’    ‘2016/12/05’<br>yyyy”年”mm”月”dd”日”‘     2016年12月05日<br><code>select hiredate,to_char(hiredate) from emp</code>–日期转换为字符的时候，不指定格式使用默认格式：dd-mm-yyyy<br><code>select hiredate,to_char(hiredate,&#39;yyyy-mm-dd&#39;) from emp;</code>–使用指定格式将日期转换为字符串类型<br><code>select hiredate,to_char(hiredate,&#39;yyyy/mm/dd&#39;) from emp;</code>–使用指定格式将日期转换为字符串类型<br><code>select hiredate,to_char(hiredate,&#39;yyyy&quot;年&quot;mm&quot;月&quot;dd&quot;日&quot;&#39;) from emp;</code>–使用指定格式将日期转换为字符串类型</li>
<li>查询入职日期在81年10月20日后的员工信息<ul>
<li>自动转型<br><code>select * from emp where hiredate&gt;&#39;20-10月-1981&#39;</code></li>
<li>将日期转换为字符串<br><code>select * from emp where to_char(hiredate,&#39;yyyy-mm-dd&#39;)&gt;&#39;1981-10-20&#39;</code></li>
<li>将日期转换为按指定格式显示的日期格式<br><code>select * from emp where hiredate&gt;to_date(&#39;1981-10-20&#39;,&#39;yyyy/mm/dd&#39;)</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="其他函数"><a href="#其他函数" class="headerlink" title="其他函数"></a>其他函数</h2><ul>
<li><strong>nvl():nvl(字段名，执行)–相当于java中的if条件判断</strong></li>
<li><strong>nvl2():nvl2(字段名，值，值)–相当于java中的If(){}else{}判断</strong></li>
<li><strong>decode():decode(字段名，条件1，执行内容1，条件2，执行内容2，条件3，执行内容3，默认执行内容)–相当于java中if(){}else if(){}else if(){}…else{}</strong></li>
<li>实例<ul>
<li>查询所有员工的月薪及姓名和工作<br><code>select * from emp</code><br><code>select ename,sal 基本工资,comm 绩效,sal+comm 月薪 from emp</code>–问题：当comm为空时null，sal+comm会显示为空，而不能正常的显示月薪。<br>  <strong><code>select ename,sal 基本工资,comm 绩效,sal+nvl(comm,0)月薪 from emp</code></strong><br>  <strong>如果comm不为空时，则sal+comm。如果comm为空时，则sal+0。</strong></li>
<li>查询所有员工的月薪及姓名和工作<br>  <strong><code>select ename,sal 基本工资,comm 绩效,nvl2(comm,sal+comm,sal)月薪 from emp</code></strong><br>  <strong>如果comm不为空null，则sal+comm。如果为空null，则sal。</strong></li>
<li>显示员工的职称<br>  <strong><code>select ename,job,decode(job,&#39;MANAGER&#39;,&#39;经理&#39;,&#39;SALESMAN&#39;,&#39;销售人员&#39;,&#39;普通员工&#39;) from emp</code></strong><br>  <strong>在job字段名中，如果job为MANAGER则显示为经理，job为SALESMAN则显示为销售人员，如果不为这两个中的任意一个，默认显示为普通人员</strong></li>
</ul>
</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Kylin</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://www.codekylin.cn/46714.html">https://www.codekylin.cn/46714.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.codekylin.cn" target="_blank">Kylin</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%9F%BA%E7%A1%80/">基础</a><a class="post-meta__tags" href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a><a class="post-meta__tags" href="/tags/Oracle/">Oracle</a><a class="post-meta__tags" href="/tags/sql/">sql</a></div><div class="post_share"><div class="social-share" data-image="https://qiniu.codekylin.cn/github/img/img/博客封面12.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://qiniu.codekylin.cn/img/20200807181442.jpg" target="_blank"><img class="post-qr-code-img" data-lazy-src="https://qiniu.codekylin.cn/img/20200807181442.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://qiniu.codekylin.cn/img/20200807181505.jpg" target="_blank"><img class="post-qr-code-img" data-lazy-src="https://qiniu.codekylin.cn/img/20200807181505.jpg" alt="支付寶"/></a><div class="post-qr-code-desc">支付寶</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/30395.html"><img class="prev-cover" data-lazy-src="https://qiniu.codekylin.cn/img/20200418114719.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Oracle数据库基础知识-3</div></div></a></div><div class="next-post pull-right"><a href="/46850.html"><img class="next-cover" data-lazy-src="https://qiniu.codekylin.cn/github/img/img/博客封面4.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info"></div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/30395.html" title="Oracle数据库基础知识-3"><img class="cover" data-lazy-src="https://qiniu.codekylin.cn/img/20200418114719.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-09-22</div><div class="title">Oracle数据库基础知识-3</div></div></a></div><div><a href="/46330.html" title="Oracle数据库基础知识-4"><img class="cover" data-lazy-src="https://qiniu.codekylin.cn/github/img/img/wallhaven-mdz9x9.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-09-22</div><div class="title">Oracle数据库基础知识-4</div></div></a></div><div><a href="/29755.html" title="Oracle数据库基础知识-5"><img class="cover" data-lazy-src="https://qiniu.codekylin.cn/github/img/img/deepin-2.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-09-22</div><div class="title">Oracle数据库基础知识-5</div></div></a></div><div><a href="/30075.html" title="Oracle数据库基础知识-6"><img class="cover" data-lazy-src="https://qiniu.codekylin.cn/github/img/img/暗红圆.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-09-22</div><div class="title">Oracle数据库基础知识-6</div></div></a></div><div><a href="/46522.html" title="Oracle数据库基础知识-7"><img class="cover" data-lazy-src="https://qiniu.codekylin.cn/github/img/img/博客封面2.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-09-26</div><div class="title">Oracle数据库基础知识-7</div></div></a></div><div><a href="/1847.html" title="Oracle数据库基础知识-10"><img class="cover" data-lazy-src="https://qiniu.codekylin.cn/github/img/img/博客封面2.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-09-26</div><div class="title">Oracle数据库基础知识-10</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" data-lazy-src="https://qiniu.codekylin.cn/img/20200807181526.jpg" onerror="this.onerror=null;this.src='https://qiniu.codekylin.cn/github/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Kylin</div><div class="author-info__description">学习不易，努力努力~</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">362</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">427</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">101</div></a></div></div><a class="button--animated" id="card-info-btn"><i class="fas fa-bookmark"></i><span>加入书签</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/kylincw" target="_blank" title="Github"><i class="iconfont icon-github"></i></a><a class="social-icon" href="tencent://message/?Menu=yes&amp;uin=171346168&amp;Service=300&amp;sigT=45a1e5847943b64c6ff3990f8a9e644d2b31356cb0b4ac6b24663a3c8dd0f8aa12a595b1714f9d45" target="_blank" title="qq"><i class="iconfont icon-qq"></i></a><a class="social-icon" href="https://space.bilibili.com/53836035" target="_blank" title="BiliBili"><i class="iconfont icon-bilibili-line"></i></a><a class="social-icon" href="mailto:zhang171346168@qq.com" target="_blank" title="Email"><i class="iconfont icon-email1"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="RSS"><i class="iconfont icon-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">学习不易，努力努力！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%95%E8%A1%8C%E5%87%BD%E6%95%B0"><span class="toc-number">1.</span> <span class="toc-text">单行函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%9A%E8%A1%8C%E5%87%BD%E6%95%B0"><span class="toc-number">2.</span> <span class="toc-text">多行函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0"><span class="toc-number">3.</span> <span class="toc-text">转换函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E5%87%BD%E6%95%B0"><span class="toc-number">4.</span> <span class="toc-text">其他函数</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/56352.html" title="be动词"><img data-lazy-src="https://qiniu.codekylin.cn/github/img/img/博客封面10.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="be动词"/></a><div class="content"><a class="title" href="/56352.html" title="be动词">be动词</a><time datetime="2022-07-12T11:47:29.800Z" title="更新于 2022-07-12 19:47:29">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/36436.html" title="JVM堆内存"><img data-lazy-src="https://qiniu.codekylin.cn/img/20200418115059.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JVM堆内存"/></a><div class="content"><a class="title" href="/36436.html" title="JVM堆内存">JVM堆内存</a><time datetime="2022-07-12T11:47:29.800Z" title="更新于 2022-07-12 19:47:29">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/44292.html" title="Java多线程详解"><img data-lazy-src="https://qiniu.codekylin.cn/github/img/img/wallhaven-eorjzk.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java多线程详解"/></a><div class="content"><a class="title" href="/44292.html" title="Java多线程详解">Java多线程详解</a><time datetime="2022-07-12T11:47:29.800Z" title="更新于 2022-07-12 19:47:29">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/40200.html" title="谷粒商城记录"><img data-lazy-src="https://qiniu.codekylin.cn/github/img/img/wallhaven-6qvvrx.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="谷粒商城记录"/></a><div class="content"><a class="title" href="/40200.html" title="谷粒商城记录">谷粒商城记录</a><time datetime="2022-07-12T11:47:29.800Z" title="更新于 2022-07-12 19:47:29">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/27082.html" title="Spring学习-3"><img data-lazy-src="https://qiniu.codekylin.cn/github/img/img/wallhaven-4x28xo.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Spring学习-3"/></a><div class="content"><a class="title" href="/27082.html" title="Spring学习-3">Spring学习-3</a><time datetime="2022-07-12T11:47:29.799Z" title="更新于 2022-07-12 19:47:29">2022-07-12</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://qiniu.codekylin.cn/github/img/img/博客封面12.png')"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2022 By Kylin</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a target="_blank" rel="noopener" href="https://beian.miit.gov.cn/"><img class="icp-icon" src="https://img.alicdn.com/tfs/TB1..50QpXXXXX7XpXXXXXXXXXX-40-40.png"><span>湘ICP备2022005420号-1</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'ClIyIUhj1ue2rcRTsApYCR50-gzGzoHsz',
      appKey: 'ATug9IScYQBHILhKWEqBHYxM',
      placeholder: '昵称填入QQ号能获取到QQ头像哦~请输入正确的邮箱地址，你将会快速收到我的回复并且通过邮件通知你！~',
      avatar: 'robohash',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'zh-cn',
      recordIP: false,
      serverURLs: 'https://cliyiuhj.lc-cn-n1-shared.com',
      emojiCDN: '',
      emojiMaps: "",
      enableQQ: true,
      path: window.location.pathname,
      requiredFields: ["nick,mail"],
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script></div></body></html>